package com.chat.netty.server.runner;

import com.chat.netty.NettyApplication;
import com.chat.netty.server.core.NettyClient;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Auther created by haoteng on 2019/08/16
 */
@Component
@Order(value = 2)
public class ClientRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {

        ExecutorService exec = Executors.newSingleThreadExecutor();
        exec.execute(new NettyClientStart());
        Thread.sleep(500);
        NettyApplication.semaphore.acquire();
        for (int i = 0; i < 10; i++) {
            ByteBuf content = Unpooled.buffer().writeBytes("channelRead Netty rocks!".getBytes());
            NettyClient.channel.writeAndFlush(content);
        }
        NettyApplication.semaphore.release();
    }

    private class NettyClientStart implements Runnable {

        @Override
        public void run() {
//            NettyClient client = new NettyClient();
//            client.start();
        }
    }
}
